import request from "../request.js";
import $var from "../var.js";
import compressImage from "./compressImage.js";

var fileInfo = {
	fileName: '',
	size: 0,
	height: 0,
	width: 0
};

var uploadFile = function(filePath, params,fileInfo) {
	const uploadTask = uni.uploadFile({
		url: $var.apiUrl + '/api/upload/img?token=GQ7G8LaZpqa1PbXrt2PZaApBM1ztJguVH246tncZ',
		filePath: filePath,
		name: 'file',
		success: (r) => {
			console.log(r)
			if(r.statusCode == 200){
				r = JSON.parse(r.data);
				// if(r.data){
				// 	params.success && params.success(r.data,fileInfo);
				// }
				if(r.key){
					params.success && params.success(r.key,fileInfo);
				}
			}
			uni.hideLoading()
		},
		fail: (resp) => console.log(resp)
	});
	if (uploadTask) {
		uploadTask.onProgressUpdate((res) => {
			uni.showLoading({
				title: res.progress + '%',
				mask: true
			});
			if(res.progress == 100) uni.hideLoading();
			params.loading && params.loading(res.progress);
			// console.log('上传进度' + res.progress);
			// console.log('已经上传的数据长度' + res.totalBytesSent);
			// console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
		});
	}
}
var uploadFile___123 = function(filePath, params,fileInfo) {
	
	request.$get({
		url: $var.qiniu.tokenUrl,
		fun(resp) {
			// console.log(resp)
			const uploadTask = uni.uploadFile({
				url: $var.qiniu.uploadUrl,
				filePath: filePath,
				name: 'file',
				formData: {
					token: resp.qiniu_upload_token
				},
				success: (resp) => {
					// console.log(resp)
					let file_path = JSON.parse(resp.data).key;
					// let fileInfo = uni.getFileInfo({
					// 	filePath: $var.fileUrl + file_path,
					// 	success(res){
					// 		console.log(res);
					// 	}
					// })
					// console.log(fileInfo);
					// return false;
					params.success && params.success(file_path,fileInfo);
					uni.hideLoading()
				},
				fail: (resp) => console.log(resp)
			});
			if (uploadTask) {
				uploadTask.onProgressUpdate((res) => {
					uni.showLoading({
						title: res.progress + '%',
						mask: true
					});
					params.loading && params.loading(res.progress);
					// console.log('上传进度' + res.progress);
					// console.log('已经上传的数据长度' + res.totalBytesSent);
					// console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
				});
			}

		}
	});
}
export default function(params) {
	console.log(111111111);
	var type = params.type || 'image'; //默认上传图片
	if (type == 'image') { //选择图片
		uni.chooseImage({
			count: params.count || 1,
			sizeType: ['compressed'],
			success(selectResp) {
				for (let index in selectResp.tempFiles) {
					let file = selectResp.tempFiles[index];
					uploadFile(file.path, params,file);
					// console.log(file);
					// return false;
					// // #ifdef H5
					// compressImage(file, function(filePath) {
					// 	uploadFile(filePath, params,file);
					// });
					// // #endif
					// // #ifndef H5
					// uploadFile(file.path, params,file);
					// // #endif
				}
			}
		})
	} else if (type == 'video') { //选择视频
		uni.chooseVideo({
			// compressed: true,
			success(file) {
				// console.log(file)
				uploadFile(file.tempFilePath, params);
			},
			fail: (resp) => console.log(resp)
		});
	}
	return false;

}
